package me.zhengjie.open.utils;

import me.zhengjie.utils.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yushaopeng
 * @Package me.zhengjie.open.utils
 * @Description:
 * @date 2025/1/9 12:21
 */
public class FireFoxHttpUtils {

    public static String blocksms(String pkey){
        String baseUrl = "http://www.firefox.fun/yhapi.ashx";
        Map<String, String> params = new HashMap<>();
        params.put("act", "addBlack");
        params.put("token", "f71d335e0bc92cd70d180f6fe49542f8_86848");

        params.put("pkey",pkey);
        params.put("reason","已注册");


        String response = "";
        try {
            String urlWithParams = appendParamsToUrl(baseUrl, params);
            response = sendGetRequest(urlWithParams);
            System.out.println("Response: " + response);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }


    public static String getSmsCode(String pkey){
        String baseUrl = "http://www.firefox.fun/yhapi.ashx";
        Map<String, String> params = new HashMap<>();
        params.put("act", "getPhoneCode");
        params.put("token", "f71d335e0bc92cd70d180f6fe49542f8_86848");

        params.put("pkey",pkey);


        String response = "";
        try {
            String urlWithParams = appendParamsToUrl(baseUrl, params);
            response = sendGetRequest(urlWithParams);
            System.out.println("Response: " + response);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }


    public static String getPhone(String country,String iid){
        String baseUrl = "http://www.firefox.fun/yhapi.ashx";
        Map<String, String> params = new HashMap<>();
        params.put("act", "getPhone");
        params.put("token", "f71d335e0bc92cd70d180f6fe49542f8_86848");
        if (!StringUtils.isEmpty(country)) {
            params.put("country", country);
        }
        params.put("dock","1");
        params.put("iid",iid);
        params.put("maxPrice","0");


        String response = "";
        try {
            String urlWithParams = appendParamsToUrl(baseUrl, params);
            response = sendGetRequest(urlWithParams);
            System.out.println("Response: " + response);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }


    private static String appendParamsToUrl(String baseUrl, Map<String, String> params) throws Exception {
        StringBuilder urlBuilder = new StringBuilder(baseUrl);
        boolean isFirstParam = true;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (!isFirstParam) {
                urlBuilder.append("&");
            } else {
                urlBuilder.append("?");
                isFirstParam = false;
            }
            urlBuilder.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.toString()))
                    .append("=")
                    .append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString()));
        }
        return urlBuilder.toString();
    }

    private static String sendGetRequest(String urlString) throws Exception {
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        int responseCode = connection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            throw new RuntimeException("HttpResponseCode: " + responseCode);
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        return response.toString();
    }
}
